package com.main.rentsystem.controller.admin;

import com.main.rentsystem.pojo.dto.HousePageQueryDTO;
import com.main.rentsystem.pojo.dto.OrderHouseDTO;
import com.main.rentsystem.pojo.entity.House;
import com.main.rentsystem.pojo.entity.PageBean;
import com.main.rentsystem.pojo.entity.Result;
import com.main.rentsystem.pojo.vo.OrderHouseVO;
import com.main.rentsystem.service.HouseServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController("adminHouseController")
@Slf4j
@RequestMapping("/admin/house")
public class HouseController {

    @Autowired
    private HouseServer houseServer;
    @Autowired
    private RedisTemplate redisTemplate;

    @GetMapping("/getHouseInfo")
    public PageBean getHouseInfo(HousePageQueryDTO housePageQueryDTO) {
        String key = "house_"
                + String.valueOf(housePageQueryDTO.getPage())
                + String.valueOf(housePageQueryDTO.getPageSize());
        //查询redis中是否存在
        PageBean pageBean = null;
        if(housePageQueryDTO.getName() == null && housePageQueryDTO.getAddress() == null && housePageQueryDTO.getFeature() == null){
            pageBean = (PageBean) redisTemplate.opsForValue().get(key);
            if(pageBean != null){
                return pageBean;
            }
        }
        //redis不存在，查询数据库
        pageBean = houseServer.pageQuery(housePageQueryDTO);
        redisTemplate.opsForValue().set(key, pageBean);

        return pageBean;
    }

    @GetMapping
    public Result saveHouse(House house) {
        log.info("新增房屋：{}",house);
        houseServer.saveHouse(house);
        cleanCache("house_*");
        return Result.success();
    }

    @PostMapping("/update")
    public Result updateHouse(@RequestBody House house) {
        log.info("修改房屋：{}",house);
        houseServer.updateHouse(house);
        cleanCache("house_*");
        return Result.success();
    }

    @DeleteMapping("/ids")
    public Result deleteById(@RequestParam List<Long> ids){
        log.info("删除房屋的编号：{}",ids);
        houseServer.deleteBatch(ids);
        cleanCache("house_*");
        return Result.success();
    }

    @GetMapping("/getOrder")
    public Result GetOrderHouse(){
        log.info("管理员查看预约看房记录");
        OrderHouseDTO orderHouseDTO = new OrderHouseDTO();
        List<OrderHouseVO> orderHouseVOList = houseServer.GetOrderHouse(orderHouseDTO);
        return Result.success(orderHouseVOList);
    }

    @GetMapping("/order/status")
    public Result updateOrderStatus(OrderHouseDTO orderHouseDTO){
        log.info("修改预约状态为：{}",orderHouseDTO.getStatus());
        houseServer.updateOrderHouse(orderHouseDTO);
        return Result.success();
    }

    //清理缓存数据
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys("house_*");
        redisTemplate.delete(keys);
    }

}
